package cn.bid.consumer.ctrl;

import cn.bid.consumer.service.UserService;
import cn.bx.pojo.AjaxResult;
import cn.bx.pojo.Page;
import cn.bx.pojo.User;
import cn.bx.pojo.Userrole;
import cn.bx.util.Md5Util;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserService userService;


    @RequestMapping(value = "userGet")
    @HystrixCommand(fallbackMethod = "userGetError",
            commandProperties = {
                    @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "2000") //超时时间，多久启动熔断
            })
    public AjaxResult userGet(@RequestParam(name="id")long id) {

        try {
            Thread.sleep((long) (2400*Math.random()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return this.userService.get(id);

    }
    public  AjaxResult userGetError(long id){
        return AjaxResult.error("服务器响应超时，稍后重试");
    }
    @RequestMapping("login1")
    public AjaxResult login( @RequestParam("username") String username, @RequestParam("password") String pass) {
      //  pass = Md5Util.getMD5(pass);
       return userService.login(username, pass);

    }


    //用户查询列表
    @GetMapping(value = "userSearch")
    @ResponseBody
    public Page search(@RequestParam(value = "page",defaultValue = "1") int pageNo, @RequestParam(value = "limit",defaultValue = "10") int limit, @RequestParam(value = "name",required = false) String name, @RequestParam(value = "startDate",required = false) Date start, @RequestParam(value = "endDate",required = false) Date end){

        return  userService.search(pageNo, limit, name, start, end);

    }

    @RequestMapping("userAdd")
    // ,@RequestParam("role")long role
    public AjaxResult userAdd(@ModelAttribute("u")User user){
      /*  Userrole userrole=new Userrole();
        userrole.setId(user.getRole());*/
        return  userService.userAdd(user);

    }
    //更新用户信息
    @RequestMapping(value = "userUpdate",produces = "application/json;utf-8")
    @ResponseBody
    public AjaxResult userUpdate(@ModelAttribute("u")User user){
        System.out.println(">>user>>>>>>"+user);
        return userService.userUpdate(user);


    }
    //删除用户
    @RequestMapping("userdel")
    @ResponseBody
    public AjaxResult del1(HttpServletRequest request, @RequestParam("id") long id){
      return  userService.del1(id);
    }
    //批量删除
    @RequestMapping(value = "batchDeluser",produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult batchDel(@RequestParam("ids[]")long[] ids){
        return   userService.batchDel(ids);

    }
    // checkUserNameRepeat
    @RequestMapping(value = "checkUserNameRepeat",produces = "application/json;charset=utf-8")
    public AjaxResult checkUserNameRepeat(@RequestParam("userName")String userName, @RequestParam(value = "id",required = false)Long id){
       return  userService.checkUserNameRepeat(userName,id);
    }
    @GetMapping(value = "findRoles",produces = "application/json;charset=utf-8")
    @ResponseBody
    public List<Userrole> findALL(){
        List<Userrole> list=   userService.findAll();
        return list;
    }

    //设置日期格式
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值
    }

}
